/**
 * 北京PK10的规则
 */
var pk10Rule = function() {
    /* 投注名称与服务器名称的对应关系
     "冠亚大" ：{
        display: "冠亚和 - 大",
     }
    */
    this.map = {

    };
    var arr = ['大', '小', '单', '双'];
    for (var i in arr) {
        this.map['冠亚' + arr[i]] = {
            display: '冠亚和 - ' + arr[i],
            rate: (arr[i] == '大' || arr[i] == '双') ? 2.19 : 1.79,
        }
    }
    for (var i = 3; i <= 19; i++) {
        this.map["冠亚" + i] = {
            display: "冠亚和 - " + i,
        };
        if (i == 3 || i == 4 || i == 18 || i == 19) this.map["冠亚" + i].rate = 42.5;
        if (i == 5 || i == 6 || i == 16 || i == 17) this.map["冠亚" + i].rate = 21.5;
        if (i == 7 || i == 8 || i == 14 || i == 15) this.map["冠亚" + i].rate = 14.5;
        if (i == 9 || i == 10 || i == 12 || i == 13) this.map["冠亚" + i].rate = 10.5;
        if (i == 11) this.map["冠亚" + i].rate = 8.5;
    }

    var arr = ['大', '小', '单', '双', '龙', '虎'];
    var arr2 = ['冠', '亚', '三', '四', '五', '六', '七', '八', '九', '十'];
    var arr3 = ['冠军', '亚军', '第三名', '第四名', '第五名', '第六名', '第七名', '第八名', '第九名', '第十名'];
    for (var i in arr2) {
        for (var j in arr) {
            if (i > 4 && j > 3) continue;
            this.map[arr2[i] + arr[j]] = {
                display: arr3[i] + " - " + arr[j],
                rate: 1.99
            }
        }
    }
    var arr4 = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
    for (var i in arr2) {
        for (var j = 1; j <= 10; j++) {
            this.map[arr2[i] + arr4[j - 1]] = {
                display: arr3[i] + " - " + j,
                rate: 9.9
            }
        }
    }
};
pk10Rule.prototype = {};
pk10Rule.prototype.construct = pk10Rule;

pk10Rule.prototype.isOdd = function(v) {
    return v % 2 == 1;
};
pk10Rule.prototype.isEven = function(v) {
    return v % 2 == 0;
}
pk10Rule.prototype.isBig = function(v) {
    return v >= 6;
}
pk10Rule.prototype.isSmall = function(v) {
    return v < 6;
}
pk10Rule.prototype.isLong = function(list, index) {
    var v1 = parseInt(list[index]),
        v2 = parseInt(list[9 - index]);
    return v1 > v2;
}
pk10Rule.prototype.isHu = function(list, index) {
    return !this.isLong(list, index);
}

/**
 * 计算露珠
 */
pk10Rule.prototype.luzhu = function(list, qiu, key) {
    // 将不是今天的开奖过滤掉
    var list2 = [];
    var today = new Date().Format("yyyy-MM-dd");
    for (var i in list) {
        if (list[i].time.startsWith(today)) list2.push(list[i]);
    }

    var self = this;
    if (key == 'count') {
        // 各数字的开奖次数
        var m = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            m[n - 1]++;
        }
        return m;
    }
    if (key == 'number') {
        // 号码排盘(最多25列)
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "daxiao") {
        // 大小
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            var dx = self.isBig(n) ? "大" : "小";
            if (m.length == 0) {
                m.push({
                    n: dx,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == dx) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: dx,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "danshuang") {
        // 单双
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            var dx = n % 2 == 0 ? "双" : "单";
            if (m.length == 0) {
                m.push({
                    n: dx,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == dx) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: dx,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "guanyahe") {
        // 冠亚和
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[0]) + parseInt(list[i].red[1]);
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "guanyahedaxiao") {
        // 冠亚和大小
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[0]) + parseInt(list[i].red[1]);
            var dx = n > 11 ? '大' : '小';
            if (m.length == 0) {
                m.push({
                    n: dx,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == dx) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: dx,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "guanyahedanshuang") {
        // 冠亚和单双
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[0]) + parseInt(list[i].red[1]);
            var dx = n % 2 == 0 ? '双' : '单';
            if (m.length == 0) {
                m.push({
                    n: dx,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == dx) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: dx,
                    c: 1,
                });
            }
        }
        return m;
    }
}

/**
 * 计算长龙信息
 */
pk10Rule.prototype.long = function(list) {
    var self = this;
    var r = [];
    if (isEmpty(list)) return r;

    // 冠亚: 大、小、单、双
    var l0 = list[0].red;
    var n = parseInt(l0[0]) + parseInt(l0[1]);

    var f = function(k, cb) {
        var t = 1;
        for (var i = 1; i < list.length; i++) {
            var l = list[i].red;
            if (!cb(l)) break;
            t++;
        }
        var oo = {}; oo[k] = t;
        if (t > 2) r.push(oo);
    };

    if (n > 11) {
        f("冠亚和 - 大", function(l) {
            var he = parseInt(l[0]) + parseInt(l[1]);
            return he > 11;
        });
    }
    else {
        f("冠亚和 - 小", function(l) {
            var he = parseInt(l[0]) + parseInt(l[1]);
            return he <= 11;
        });
    }
    if (self.isOdd(n)) {
        f("冠亚和 - 单", function(l) {
            var he = parseInt(l[0]) + parseInt(l[1]);
            return self.isOdd(he);
        });
    }
    else {
        f("冠亚和 - 双", function(l) {
            var he = parseInt(l[0]) + parseInt(l[1]);
            return !self.isOdd(he);
        });
    }

    // 1-10名大小、单双
    var arr = ["冠军", "亚军", "第三名", "第四名", "第五名", "第六名", "第七名", "第八名", "第九名", "第十名"];
    for (var index = 0; index < 10; index++) {
        var n = parseInt(l0[index]);
        if (self.isBig(n)) {
            f(arr[index] + " - 大", function(l) {
                return self.isBig(parseInt(l[index]));
            });
        }
        else {
            f(arr[index] + " - 小", function(l) {
                return !self.isBig(parseInt(l[index]));
            });
        }
        if (self.isOdd(n)) {
            f(arr[index] + " - 单", function(l) {
                return self.isOdd(parseInt(l[index]));
            });
        }
        else {
            f(arr[index] + " - 双", function(l) {
                return !self.isOdd(parseInt(l[index]));
            });
        }
        if (index >= 5) continue;

        if (self.isLong(l0, index)) {
            f(arr[index] + " - 龙", function(l) {
                return self.isLong(l, index);
            });
        }
        else {
            f(arr[index] + " - 虎", function(l) {
                return !self.isLong(l, index);
            });
        }
    }

    // 排序
    r.sort(function(a, b) {
        var v1, v2;
        for (var k in a) v1 = a[k];
        for (var k in b) v2 = b[k];
        return v2 - v1;
    });
    return r;
}

/**
 * 转换显示的名称(例如：冠大 -> 冠军大)
 */
pk10Rule.prototype.getTargetName = function(name) {
    return this.map[name] ? this.map[name].display : name;
};

window.PK10Rule = new pk10Rule();
